﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IronOne.SalesOne.BusinessObjects;
using Facade;
using System.Data;
using System.Configuration;


namespace IronOne.SalesOne.SalesOneWeb
{
    public partial class ManageGroups : SOSecurityPage, IDataPage, IntTest
    {
        private int pageNo = 1;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    ValidateUserCredentials(Request.Url.LocalPath);
                    //UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                }

                lblMessage.Text = "";
                //btnManageGrpUsers.Visible = false;
                //btnMngBranchUseres.Visible = false;

                if (Session["sccGrp"] != null)
                {
                    MassageSummery("IIS401");
                    //UCGrid1.SetText("1"); 
                    //lblMessage.Text = Session["sccGrp"].ToString();
                    //lblMessage.ForeColor = System.Drawing.Color.Green;
                    Session.Remove("sccGrp");
                }

                if (Session["failGrp"] != null)
                {
                    MassageSummery("IIS502");
                    //lblMessage.Text = Session["failGrp"].ToString();
                    //lblMessage.ForeColor = System.Drawing.Color.Red;
                    Session.Remove("failGrp");
                }



                string postCause = this.Page.Request.Params.Get("__EVENTTARGET");

                if (postCause == null)
                {
                    if (Session["sGrp"] != null)
                    {
                        UCGrid1.SetText("1");
                        Search(Session["sGrp"].ToString(), 1);
                        Session.Remove("sGrp");
                    }

                    //postCause = "REDIRECT";
                }
                else if (postCause.Contains("SiteMenu"))
                {
                    if (Session["sGrp"] != null)
                    {
                        Session.Remove("sGrp");
                    }
                }
                else if (postCause.Contains("Radio"))
                {
                    //UCGrid1 = (UserControls.UCUserGrid)Session["ucg"];
                    //    UCGrid1.styleList = new List<GridViewStyle> {
                    //    new GridViewStyle{
                    //        ColumnId = "Name",
                    //        ColumnHeader = "Group Name",
                    //        ColumnWidth = 130 },
                    //    new GridViewStyle{
                    //        ColumnId = "EffectFrom",
                    //        ColumnHeader = "Effective From",
                    //        ColumnWidth = 130},                
                    //    new GridViewStyle{
                    //        ColumnId = "EffectTo",
                    //        ColumnHeader = "Effective To",
                    //        ColumnWidth = 130}

                    //};

                }
            }
            catch (Exception)
            {

            }
            

            //if (string.IsNullOrEmpty(postCause))
            //{
            //    postCause = "REDIRECT";
            //}


            //if (!postCause.Contains("RadioButton1"))
            //{
            //    UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
            //    btnManageGrpUsers.Visible = true;
            //    btnMngBranchUseres.Visible = true;
            //    string grpName = "%" + txtGroupName.Text + "%";
            //    StructureFacade fac = new StructureFacade();
            //    List<SalesOneGroup> groupList = fac.GetSearchGroups(grpName);



            //    if (groupList != null)
            //    {
            //        btnCreate.Visible = false;
            //        UCGridButtons1.Visible = true;
            //    }
            //    else
            //    {

            //    }

            //    int[] numbers = { 1, 2 };
            //    UCGrid1.SetDataSource(groupList, numbers);


            //}

            //if (!IsPostBack)
            //{
            //    UCGridButtons1.Visible = false;

                
            //}

            //UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
            /*UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "Name",
                    ColumnHeader = "Group Name",
                    ColumnWidth = 130 },
                new GridViewStyle{
                    ColumnId = "EffectFrom",
                    ColumnHeader = "Effective From",
                    ColumnWidth = 130},                
                new GridViewStyle{
                    ColumnId = "EffectTo",
                    ColumnHeader = "Effective To",
                    ColumnWidth = 130}
                
            };*/
        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pageNo = 1;
            Search(string.Empty,1);
            //Session["ucg"] = UCGrid1;
            
            
        }


        public void Search(string gn,int page)
        {
            try
            {
                string grpName = string.Empty;

                if (string.IsNullOrEmpty(gn))
                {
                    grpName = "%" + txtGroupName.Text + "%";

                    if (page == 1)
                    {
                        UCGrid1.SetText("1");
                        Session["sGrp"] = grpName;
                    }

                }
                else
                {
                    grpName = gn;
                }



                StructureFacade fac = new StructureFacade();
                List<SalesOneGroup> groupList = fac.GetSearchGroups(grpName, page);


                int[] numbers = { 1, 2, 3, 7 };

                UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "Name",
                    ColumnHeader = "Group Name",
                    ColumnWidth = 130 },
                new GridViewStyle{
                    ColumnId = "EffectFrom",
                    ColumnHeader = "Effective From",
                    ColumnWidth = 130},                
                new GridViewStyle{
                    ColumnId = "EffectTo",
                    ColumnHeader = "Effective To",
                    ColumnWidth = 130}
                
            };


                

                if (groupList != null && groupList.Count > 0)
                {
                    btnCreate.Visible = false;
                    UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                    btnManageGrpUsers.Visible = true;
                    btnMngBranchUseres.Visible = true;

                    UCGridButtons1.Visible = true;

                    if (groupList.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                    {
                        UCGrid1.IsLastPage = (fac.GetSearchGroups(grpName, page + 1)).Count > 0 ? false : true;
                    }

                    UCGrid1.SetDataSource(groupList, numbers);
                }
                else
                {
                    btnCreate.Visible = true;
                    //lblMessage.Text = "No groups to display";
                    MassageSummery("IIS504");
                    //lblMessage.ForeColor = System.Drawing.Color.Red;

                    btnManageGrpUsers.Visible = false;
                    btnMngBranchUseres.Visible = false;

                    UCGridButtons1.Visible = false;
                }
            }
            catch (Exception)
            {

            }
            

            
        }


        #region IDataPage Members

        public void Create()
        {
            Session["sGrp"] = "%" + txtGroupName.Text + "%";
            Response.Redirect("~/UserManagement/AddGroup.aspx");
        }

        public void View()
        {
            try
            {
                Session["sGrp"] = "%" + txtGroupName.Text + "%";

                if (Session["groupId"] == null)
                {

                    Session["groupId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
                    Session.Remove("firstRow");
                }

                if (Session["groupId"] != null)
                {
                    Response.Redirect("~/UserManagement/AddGroup.aspx?a=v");
                }
                else
                {
                    MassageSummery("IIS503");
                    //lblMessage.Text = "Please select a group";
                    //lblMessage.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception)
            {

            }
                       
        }

        public void Update()
        {
            try
            {
                Session["sGrp"] = "%" + txtGroupName.Text + "%";

                if (Session["groupId"] == null)
                {

                    Session["groupId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
                    Session.Remove("firstRow");
                }

                if (Session["groupId"] != null)
                {
                    Response.Redirect("~/UserManagement/AddGroup.aspx?a=u");
                }
                else
                {
                    MassageSummery("IIS503");
                    //lblMessage.Text = "Please select a group";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception)
            {

            }
            
        }

        public void Delete()
        {

            Session["sGrp"] = "%" + txtGroupName.Text + "%";

            if (Session["groupId"] == null)
            {
                if (Session["firstRow"] != null)
                {
                    Session["groupId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
                    Session.Remove("firstRow");
                }
                else
                {
                    MassageSummery("IIS503");
                }
                
            }

            try
            {
                if (Session["groupId"] != null)
                {
                    StructureFacade fac = new StructureFacade();
                    string grp = Session["groupId"].ToString();
                    SalesOneGroup gr = fac.GetGroup(Convert.ToInt32(grp));
                    int s = fac.DeleteGroup(gr);

                    if (s == 1)
                    {
                        UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                        btnManageGrpUsers.Visible = true;
                        btnMngBranchUseres.Visible = true;
                        string grpName = "%" + txtGroupName.Text + "%";
                        List<SalesOneGroup> groupList = fac.GetSearchGroups(grpName, 1);



                        if (groupList != null)
                        {
                            UCGridButtons1.Visible = true;
                        }

                        int[] numbers = { 1, 2 };


                        UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "Name",
                    ColumnHeader = "Group Name",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "EffectFrom",
                    ColumnHeader = "Effective From",
                    ColumnWidth = 130},                
                new GridViewStyle{
                    ColumnId = "EffectTo",
                    ColumnHeader = "Effective To",
                    ColumnWidth = 130}
                
            };

                        UCGrid1.SetDataSource(groupList, numbers);

                        Session.Remove("groupId");
                        MassageSummery("IIS403");
                    }
                    else
                    {
                        MassageSummery("IIS545");
                    }
                    
                   
                }
                else
                {
                    MassageSummery("IIS503");
                    //lblMessage.Text = "Please select a group";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                }
               
            }
            catch (Exception)
            {
                
            }
            
        }

        public void Cancel()
        {

        }
        

        #endregion



        public void GetId(GridViewRow row)
        {
            Session["groupId"] = row.Cells[2].Text;
        }


        protected void btnManageGrpUsers_Click1(object sender, EventArgs e)
        {
            try
            {
                Session["sGrp"] = "%" + txtGroupName.Text + "%";

                if (Session["groupId"] == null)
                {

                    Session["groupId"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
                    Session.Remove("firstRow");
                }


                if (Session["groupId"] != null)
                {
                    Response.Redirect("~/UserManagement/ManageUsers.aspx?x=group");
                }
                else
                {
                    MassageSummery("IIS503");
                    //lblMessage.Text = "Please select a group";
                    //lblMessage.ForeColor = System.Drawing.Color.Red;
                }
                //GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

                //foreach (GridViewRow row in gr.Rows)
                //{
                //    RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                //    if (rbtn.Checked)
                //    {
                //        Response.Redirect("~/UserManagement/ManageUsers.aspx?x=group&grpId=" + row.Cells[2].Text);
                //    }
                //    else
                //    {
                //        MassageSummery("IIS503");
                //    }
                //}
            }
            catch (Exception)
            {

            }
            
        }

        protected void btnMngBranchUseres_Click1(object sender, EventArgs e)
        {
            try
            {
                Session["sGrp"] = "%" + txtGroupName.Text + "%";

                GridView gr = (GridView)UCGrid1.FindControl("grdUsers");

                foreach (GridViewRow row in gr.Rows)
                {
                    RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                    if (rbtn.Checked)
                    {
                        Response.Redirect("~/Structure/ManageBranches.aspx?grpId=" + row.Cells[2].Text);
                    }
                    else
                    {
                        MassageSummery("IIS503");
                    }
                }
            }
            catch (Exception)
            {

            }

        }



        #region IDataPage Members


        public void Next()
        {
            try
            {
                if (UCGrid1.GetText() != "1")
                {
                    pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                    UCGrid1.SetText(pageNo.ToString());

                }
                else
                {
                    pageNo = 2;
                    UCGrid1.SetText("2");

                }

                Search(string.Empty, pageNo);
            }
            catch (Exception)
            {

            }
            
        }

        public void Previous()
        {
            try
            {
                if (UCGrid1.GetText() != "1")
                {
                    pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                    UCGrid1.SetText(pageNo.ToString());

                    Search(string.Empty, pageNo);
                }
            }
            catch (Exception)
            {

            }
            
        }

        public void Go()
        {

                int p = int.Parse(UCGrid1.GetText());
                if (p != 0)
                {
                    try
                    {
                        Search(Session["sGrp"].ToString(), p);
                        pageNo = p;
                    }
                    catch (Exception ex)
                    {

                        //Response.Write(ex.Message);
                    }


                }
            
            
        }

        #endregion

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            
        }

        protected void btnCreate_Click1(object sender, EventArgs e)
        {
            Response.Redirect("~/UserManagement/AddGroup.aspx");
        }

       
    }
}
